package com.cskaoyan.controller.wx;

import com.cskaoyan.bean.bo.WxglMallManagementControllerBo.RegisterUser;
import com.cskaoyan.bean.bo.wjjBO.WXAuthResetBO;
import com.cskaoyan.bean.marketBean.MarketUser;
import com.cskaoyan.mapper.marketMapper.MarketUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @author wjj
 * @date 2022-12-31 22:08
 * @describe
 */
@Service
@Transactional
public class WXAuthServiceImpl implements WXAuthService {

    @Autowired
    MarketUserMapper userMapper;

    @Override
    public int updateUserPasswordByMobile(WXAuthResetBO authResetBO) {
        MarketUser marketUser = new MarketUser();
        marketUser.setMobile(authResetBO.getMobile());
        marketUser.setPassword(authResetBO.getPassword());
        int affectRows = userMapper.updateUserPasswordByMoile(marketUser);
        if (affectRows == 1){
            return 200;
        }else {
            return 404;
        }
    }

    /**
     * @Author xiaowu
     * @Description // 注册
     * @Date 16:50 2023/1/2
     * @Param []  {"username":"user222","password":"user222","mobile":"13476606183","code":"333542","wxCode":"the code is a mock one"}
     * @return {"errno":705,"errmsg":"手机号已注册"} //{"errno":200,"errmsg":"注册成功"} //{"errno":400,"errmsg":"验证码错误"}
     **/
    @Override
    public int authRegisterUser(RegisterUser registerUser) {
        String mobile = registerUser.getMobile();
        int rows = userMapper.selectCountFromUserByMobile(mobile);
        if (rows != 0){
            return 10000;
        }
        MarketUser marketUser = new MarketUser();
        marketUser.setUsername(registerUser.getUsername());
        marketUser.setPassword(registerUser.getPassword());
        marketUser.setMobile(registerUser.getMobile());
        marketUser.setAddTime(new Date());
        marketUser.setUpdateTime(new Date());

        userMapper.insertRegisterUser(marketUser);
        return 200;
    }
}
